<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryForm" :inline="true" v-show="showSearch" label-width="68px">
      <el-form-item label="小区" prop="areaName">
        <el-select v-model="queryParams.areaName":filter-method="filterMethod" placeholder="请输入小区" filterable allow-create
                   clearable size="small"  default-first-option>
          <el-option v-for="(item, index) in areaIdOptions" :key="index" :label="item.areaName"
                     :value="item.areaName" :disabled="item.disabled" ></el-option>
        </el-select>
      </el-form-item>
      <el-form-item>
        <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>

    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button
          type="warning"
          plain
          size="mini"
          icon="el-icon-download"
          @click="handleExport"
        >导出(小区)</el-button>
      </el-col>
    </el-row>


    <el-table v-loading="loading" :data="collectQueryList" >
      <el-table-column label="小区" align="center" prop="areaName" width="120"/>
      <el-table-column :formatter="numberFormat"label="总户数" align="center" prop="doorSum" width="100"/>
      <el-table-column :formatter="numberFormat"label="总面积" align="center" prop="spaceSum" width="120"/>
      <el-table-column  :formatter="numberFormat"  label="应缴金额" align="center" prop="budgetMoney" width="120"/>
      <el-table-column  :formatter="numberFormat"  label="已缴金额" align="center" prop="costMoney" width="120"/>
      <el-table-column  :formatter="numberFormat"  label="使用金额" align="center" prop="payMoney" width="120"/>
      <el-table-column  :formatter="numberFormat"  label="本金余额" align="center" prop="balance" width="120"/>
      <el-table-column  :formatter="numberFormat"  label="利息合计" align="center" prop="rateMoney" width="120"/>

      <el-table-column label="操作" align="center"  fixed="right"  class-name="small-padding fixed-width">
        <template slot-scope="scope">
          <el-button
            type="text"
            size="mini"
            icon="el-icon-search"
            @click="costQuery(scope.row)"
            :style="{ display: scope.row.areaName=='合计'?'none':'' }"
          >缴费明细</el-button>
          <el-dropdown size="mini" @command="(command) => handleCommand(command, scope.row)" :style="{ display: scope.row.areaName=='合计'?'none':'' }"
                       v-hasPermi="['system:role:edit']">
            <span class="el-dropdown-link">
              <i class="el-icon-d-arrow-right el-icon--right"></i>更多
            </span>
            <el-dropdown-menu slot="dropdown">
              <el-dropdown-item command="rateQuery" icon="el-icon-search"
                                    >利息明细</el-dropdown-item>
              <el-dropdown-item command="repairQuery" icon="el-icon-search"
                                    >分摊明细</el-dropdown-item>
            </el-dropdown-menu>
          </el-dropdown>
        </template>
      </el-table-column>
    </el-table>

    <pagination
      v-show="total>0"
      :total="total"
      :page.sync="queryParams.pageNum"
      :limit.sync="queryParams.pageSize"
      @pagination="getList"
    />


    <!-- 查看缴费记录 -->
    <el-dialog :title="title" :visible.sync="openCost" width="1000px" append-to-body>
      <el-form :model="queryParams1" :inline="true" v-show="showSearch" label-width="68px">
        <el-table v-loading="loading" :data="costQueryList">
          <el-table-column label="地址" align="center" prop="doorAddr" width="200"/>
          <el-table-column label="房屋编号" align="center" prop="houseId" />
          <el-table-column :formatter="numberFormat"label="缴费金额" align="center" prop="costMoney" />
          <el-table-column label="缴费时间" align="center" prop="costDate" width="180">
            <template slot-scope="scope">
              <span>{{ parseTime(scope.row.costDate, '{y}-{m}-{d}') }}</span>
            </template>
          </el-table-column>
          <el-table-column label="姓名" align="userName" prop="userName" />
        </el-table>
        <pagination
          v-show="total>0"
          :total="total1"
          :page.sync="queryParams1.pageNum"
          :limit.sync="queryParams1.pageSize"
          @pagination="getCostList"
        />
      </el-form>
    </el-dialog>

    <!-- 查看计息明细记录 -->
    <el-dialog :title="title" :visible.sync="openRate" width="1000px" append-to-body>
      <el-form :model="queryParams2" :inline="true" v-show="showSearch" label-width="68px">
        <el-table v-loading="loading" :data="rateQueryList">
          <el-table-column label="地址" align="center" prop="doorAddr" width="200"/>
          <el-table-column label="房屋编号" align="center" prop="houseId" />
          <el-table-column :formatter="numberFormat" label="利息合计" align="center" prop="rateMoney"/>
          <el-table-column label="计息日" align="center" prop="rateDate">
            <template slot-scope="scope">
              <span>{{ parseTime(scope.row.rateDate, '{y}-{m}-{d}') }}</span>
            </template>
          </el-table-column>
        </el-table>
        <pagination
          v-show="total>0"
          :total="total2"
          :page.sync="queryParams2.pageNum"
          :limit.sync="queryParams2.pageSize"
          @pagination="getRateList"
        />
      </el-form>
    </el-dialog>

    <!-- 查看分摊明细记录 -->
    <el-dialog :title="title" :visible.sync="openRepair" width="1000px" append-to-body>
      <el-form :model="queryParams3" :inline="true" v-show="showSearch" label-width="68px">
        <el-table v-loading="loading" :data="repairQueryList">
          <el-table-column label="工程编号" align="center" prop="projectId"  width="150"/>
          <el-table-column label="地址" align="center" prop="doorAddr" width="200"/>
          <el-table-column label="房屋编号" align="center" prop="houseId" />
          <el-table-column label="姓名" align="userName" prop="userName" />
          <el-table-column :formatter="numberFormat" label="当前余额" align="center" prop="balance" />
          <el-table-column :formatter="numberFormat" label="扣款金额" align="center" prop="payMoney" />
          <el-table-column :formatter="numberFormat" label="不足金额" align="center" prop="noMoney" />
          <el-table-column label="分摊日期" align="center" prop="executeDate">
            <template slot-scope="scope">
              <span>{{ parseTime(scope.row.executeDate, '{y}-{m}-{d}') }}</span>
            </template>
          </el-table-column>
        </el-table>
        <pagination
          v-show="total>0"
          :total="total3"
          :page.sync="queryParams3.pageNum"
          :limit.sync="queryParams3.pageSize"
          @pagination="getRepairList"
        />
      </el-form>
    </el-dialog>

  </div>
</template>

<script>
import { listCollectQuery} from "@/api/report/collectQuery";
import {listCostQuery,listRateQuery,listRepairQuery} from "@/api/report/moneyFlow";
import {getAreaInfo,getAreaInfo1} from "@/api/house/area";

export default {
  name: "CollectQuery",
  data() {
    return {
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
/*      // 日期范围
      dateRange: [],*/
      // 总条数
      total: 0,
      total1: 0,
      total2: 0,
      total3: 0,
      // 缴费信息表格数据
      collectQueryList: [],
      costQueryList: [],
      rateQueryList: [],
      repairQueryList: [],
      areaIdOptions:[{}],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      openCost: false,
      openRate: false,
      openRepair: false,
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        houseId: null,
        areaName:null,
        costDate: null,
        spaceSum: null,
        doorSum: null,
        costSts: null,
        costWay: null,
        checkSts: null,
        billNo: null,
      },
      // 查询缴费参数
      queryParams1: {
        pageNum: 1,
        pageSize: 10,
        houseId: null,
        areaName: null,
        costDate: null,
        unitName: null,
        doorName: null,
        groupName: null,
      },
      // 查询计息参数
      queryParams2: {
        pageNum: 1,
        pageSize: 10,
        houseId: null,
        rateMoney: null,
        rateDate: null
      },
      // 查询分摊参数
      queryParams3: {
        pageNum: 1,
        pageSize: 10,
        Name: null,
        groupName: null,
        unitName: null,
        doorName: null,
        areaName: null,
        houseId: null,
        projectId: null,
        executeDate: null,
        payMoney: null,
        noMoney: null,
        balance: null,
        startDate:null,
        endDate:null,
      },
      // 表单参数
      form: {},
      // 表单校验
      rules: {
      }
    };
  },
  created() {
    this.getList();
    this.filterMethod();
    this.areaSelect();
  },
  methods: {
    numberFormat (row, column, cellValue) {
      cellValue += ''
      if (!cellValue.includes('.')) cellValue += '.'
      return cellValue.replace(/(\d)(?=(\d{3})+\.)/g, function ($0, $1) {
        return $1 + ','
      }).replace(/\.$/, '')
    },

    /** 查询缴费信息列表 */
    getCostList() {
      this.loading = true;
      listCostQuery(this.queryParams1).then(response => {
        this.costQueryList = response.rows;
        this.total1 = response.total;
        this.loading = false;
      });
    },

    /** 查询计息明细列表 */
    getRateList() {
      this.loading = true;
      listRateQuery(this.queryParams2).then(response => {
        this.rateQueryList = response.rows;
        this.total2 = response.total;
        this.loading = false;
      });
    },
    /** 查询计息明细列表 */
    getRepairList() {
      this.loading = true;
      listRepairQuery(this.queryParams3).then(response => {
        this.repairQueryList = response.rows;
        this.total3 = response.total;
        this.loading = false;
      });
    },

    // 更多操作触发
    handleCommand(command, row) {
      switch (command) {
        case "rateQuery":
          this.rateQuery(row);
          break;
        case "repairQuery":
          this.repairQuery(row);
          break;
        default:
          break;
      }
    },
    costQuery(row) {
      const areaName = row.areaName;
      var startDate=this.queryParams.startDate;
      var endDate=this.queryParams.endDate;
      this.queryParams1.areaName=areaName
      this.queryParams1.startDate=startDate
      this.queryParams1.endDate=endDate
      this.openCost = true;
      this.getCostList()
    },
    repairQuery(row) {
      const areaName = row.areaName;
      var startDate=this.queryParams.startDate;
      var endDate=this.queryParams.endDate;
      this.queryParams3.areaName=areaName
      this.queryParams3.startDate=startDate
      this.queryParams3.endDate=endDate
      this.openRepair = true;
      this.getRepairList()
    },
    /** 获取计息记录 */
    rateQuery(row) {
      const areaName = row.areaName;
      var startDate=this.queryParams.startDate;
      var endDate=this.queryParams.endDate;
      this.queryParams2.areaName=areaName
      this.queryParams2.startDate=startDate
      this.queryParams2.endDate=endDate
      this.openRate = true;
      this.getRateList()
    },
    filterMethod(val){
      this.value=val
      getAreaInfo1(this.value).then(response => {
        this.areaIdOptions = response.rows;
      });
    },
    areaSelect(){
      getAreaInfo().then(response => {
        this.areaIdOptions = response.rows;
      });
    },
    /** 查询缴费信息列表 */
    getList() {
      this.loading = true;
      listCollectQuery(this.queryParams).then(response => {
        this.collectQueryList = response.rows;
        this.total = response.total;
        this.loading = false;
        this.areaSelect();
      });
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm("queryForm");
      this.handleQuery();
    },
    /** 导出按钮操作 */
    handleExport() {
      this.download('/report/collectQuery/export', {
        ...this.queryParams
      }, `导出（小区）_${new Date().getTime()}.xlsx`)
    },
  }
};
</script>
